<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/echoplugin.qdoc -->
<head>
  <title>Qt 4.3: Echo Plugin Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Echo Plugin Example<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="tools-echoplugin-echowindow-echointerface-h.html">tools/echoplugin/echowindow/echointerface.h</a></li>
<li><a href="tools-echoplugin-echowindow-echowindow-cpp.html">tools/echoplugin/echowindow/echowindow.cpp</a></li>
<li><a href="tools-echoplugin-echowindow-echowindow-h.html">tools/echoplugin/echowindow/echowindow.h</a></li>
<li><a href="tools-echoplugin-plugin-echoplugin-cpp.html">tools/echoplugin/plugin/echoplugin.cpp</a></li>
<li><a href="tools-echoplugin-plugin-echoplugin-h.html">tools/echoplugin/plugin/echoplugin.h</a></li>
<li><a href="tools-echoplugin-echowindow-main-cpp.html">tools/echoplugin/echowindow/main.cpp</a></li>
</ul>
<p>This example shows how to create a Qt plugin.</p>
<p align="center"><img src="images/echopluginexample.png" /></p><p>There are two kinds of plugins in Qt: plugins that extend Qt itself and plugins that extend applications written in Qt. In this example, we show the procedure of implementing plugins that extend applications. When you create a plugin you declare an interface, which is a class with only pure virtual functions. This interface is inherited by the class that implements the plugin. The class is stored in a shared library and can therefore be loaded by applications at run-time. When loaded, the plugin is dynamically cast to the interface using Qt's <a href="metaobjects.html">meta-object system</a>. The plugin <a href="plugins-howto.html">overview document</a> gives a high-level introduction to plugins.</p>
<p>We have implemented a plugin, the <tt>EchoPlugin</tt>, which implements the <tt>EchoInterface</tt>. The interface consists of <tt>echo()</tt>, which takes a <a href="qstring.html">QString</a> as argument. The <tt>EchoPlugin</tt> returns the string unaltered (i.e&#x2e;, it works as the familiar echo command found in both Unix and Windows).</p>
<p>We test the plugin in <tt>EchoWindow</tt>: when you push the <a href="qpushbutton.html">QPushButton</a> (as seen in the image above), the application sends the text in the <a href="qlineedit.html">QLineEdit</a> to the plugin, which echoes it back to the application. The answer from the plugin is displayed in the <a href="qlabel.html">QLabel</a>.</p>
<a name="echowindow-class-definition"></a>
<h2>EchoWindow Class Definition</h2>
<p>The <tt>EchoWindow</tt> class lets us test the <tt>EchoPlugin</tt> through a GUI.</p>
<pre> class EchoWindow : public QWidget
 {
     Q_OBJECT

 public:
     EchoWindow();

 private slots:
     void sendEcho();

 private:
     void createGUI();
     bool loadPlugin();

     EchoInterface *echoInterface;
     QLineEdit *lineEdit;
     QLabel *label;
     QPushButton *button;
     QGridLayout *layout;
 };</pre>
<p>We load the plugin in <tt>loadPlugin()</tt> and cast it to <tt>EchoInterface</tt>. When the user clicks the <tt>button</tt> we take the text in <tt>lineEdit</tt> and call the interface's <tt>echo()</tt> with it.</p>
<a name="echowindow-class-implementation"></a>
<h2>EchoWindow Class Implementation</h2>
<p>We start with a look at the constructor:</p>
<pre> EchoWindow::EchoWindow()
 {
     createGUI();
     setLayout(layout);
     setWindowTitle(&quot;Echo Plugin Example&quot;);

     if (!loadPlugin()) {
         QMessageBox::information(this, &quot;Error&quot;, &quot;Could not load the plugin&quot;);
         lineEdit-&gt;setEnabled(false);
         button-&gt;setEnabled(false);
     }
 }</pre>
<p>We create the widgets and set a title for the window. We then load the plugin. <tt>loadPlugin()</tt> returns false if the plugin could not be loaded, in which case we disable the widgets. If you wish a more detailed error message, you can use <a href="qpluginloader.html#errorString">errorString()</a>; we will look more closely at <a href="qpluginloader.html">QPluginLoader</a> later.</p>
<p>Here is the implementation of <tt>sendEcho()</tt>:</p>
<pre> void EchoWindow::sendEcho()
 {
     QString text = echoInterface-&gt;echo(lineEdit-&gt;text());
     label-&gt;setText(text);
 }</pre>
<p>This slot is called when the user pushes <tt>button</tt> or presses enter in <tt>lineEdit</tt>. We call <tt>echo()</tt> of the echo interface. In our example this is the <tt>EchoPlugin</tt>, but it could be any plugin that inherit the <tt>EchoInterface</tt>. We take the <a href="qstring.html">QString</a> returned from <tt>echo()</tt> and display it in the <tt>label</tt>.</p>
<p>Here is the implementation of <tt>createGUI()</tt>:</p>
<pre> void EchoWindow::createGUI()
 {
     lineEdit = new QLineEdit;
     label = new QLabel;
     label-&gt;setFrameStyle(QFrame::Box | QFrame::Plain);
     button = new QPushButton(tr(&quot;Send Message&quot;));

     connect(lineEdit, SIGNAL(editingFinished()),
             this, SLOT(sendEcho()));
     connect(button, SIGNAL(clicked()),
             this, SLOT(sendEcho()));

     layout = new QGridLayout;
     layout-&gt;addWidget(new QLabel(tr(&quot;Message:&quot;)), 0, 0);
     layout-&gt;addWidget(lineEdit, 0, 1);
     layout-&gt;addWidget(new QLabel(tr(&quot;Answer:&quot;)), 1, 0);
     layout-&gt;addWidget(label, 1, 1);
     layout-&gt;addWidget(button, 2, 1, Qt::AlignRight);
     layout-&gt;setSizeConstraint(QLayout::SetFixedSize);
 }</pre>
<p>We create the widgets and lay them out in a grid layout. We connect the label and line edit to our <tt>sendEcho()</tt> slot.</p>
<p>Here is the <tt>loadPlugin()</tt> function:</p>
<pre> bool EchoWindow::loadPlugin()
 {
     QDir pluginDirectory(qApp-&gt;applicationDirPath() + &quot;/plugin/&quot;);
     QString fileName = pluginDirectory.entryList(
         QStringList() &lt;&lt; &quot;libechoplugin*&quot;).first();

     QPluginLoader pluginLoader(pluginDirectory.absoluteFilePath(fileName));
     QObject *plugin = pluginLoader.instance();
     if (plugin) {
         echoInterface = qobject_cast&lt;EchoInterface *&gt;(plugin);
     if(echoInterface)
         return true;
     }
     return false;
 }</pre>
<p>Access to plugins at run-time is provided by <a href="qpluginloader.html">QPluginLoader</a>. You supply it with the filename of the shared library the plugin is stored in and call <a href="qpluginloader.html#instance">instance()</a>, which loads and returns the root component of the plugin (i.e&#x2e;, it resolves the type of the plugin and creates a <a href="qobject.html">QObject</a> instance of it). If the plugin was not successfully loaded, it will be null, so we return false. If it was loaded correctly, we can cast the plugin to our <tt>EchoInterface</tt> and return true. In the case that the plugin loaded does not implement the <tt>EchoInterface</tt>, <tt>instance()</tt> will return null, but this cannot happen in our example.</p>
<a name="echointerface-class-definition"></a>
<h2>EchoInterface Class Definition</h2>
<p>The <tt>EchoInterface</tt> defines the functions that the plugin will provide. An interface is a class that only consists of pure virtual functions. If non virtual functions were present in the class you would get misleading compile errors in the moc files.</p>
<pre> class EchoInterface
 {
 public:
     virtual ~EchoInterface() {}
     virtual QString echo(const QString &amp;message) = 0;
 };

 Q_DECLARE_INTERFACE(EchoInterface,
                     &quot;com.trolltech.Plugin.EchoInterface/1.0&quot;);</pre>
<p>We declare <tt>echo()</tt>. In our <tt>EchoPlugin</tt> we use this method to return, or echo, <i>message</i>.</p>
<p>We use the Q_DECLARE_INTERFACE macro to let <a href="metaobjects.html">Qt's meta object system</a> aware of the interface. We do this so that it will be possible to identify plugins that implements the interface at run-time. The second argument is a string that must identify the interface in a unique way.</p>
<a name="echoplugin-class-definition"></a>
<h2>EchoPlugin Class Definition</h2>
<p>We inherit both <a href="qobject.html">QObject</a> and <tt>EchoInterface</tt> to make this class a plugin. The Q_INTERFACES macro tells Qt which interfaces the class implements. In our case we only implement the <tt>EchoInterface</tt>. If a class implements more than one interface, they are given as a comma separated list.</p>
<pre> class EchoPlugin : public QObject, EchoInterface
 {
     Q_OBJECT
     Q_INTERFACES(EchoInterface)

 public:
     QString echo(const QString &amp;message);
 };</pre>
<a name="echoplugin-class-implementation"></a>
<h2>EchoPlugin Class Implementation</h2>
<p>Here is the implementation of <tt>echo()</tt>:</p>
<pre> QString EchoPlugin::echo(const QString &amp;message)
 {
     return message;
 }</pre>
<p>We simply return the functions parameter.</p>
<pre> Q_EXPORT_PLUGIN2(echoplugin, EchoPlugin);</pre>
<p>We use the <a href="qtplugin.html#Q_EXPORT_PLUGIN2#q-export-plugin2">Q_EXPORT_PLUGIN2</a> macro to let Qt know that the <tt>EchoPlugin</tt> class is a plugin. The first parameter is the name of the plugin; it is usual to give the plugin and the library file it is stored in the same name.</p>
<a name="the-function"></a>
<h2>The <tt>main()</tt> function</h2>
<pre> int main(int argv, char *args[])
 {
     QApplication app(argv, args);

     EchoWindow window;
     window.show();

     return app.exec();
 }</pre>
<p>We create an <tt>EchoWindow</tt> and display it as a top-level window.</p>
<a name="the-profiles"></a>
<h2>The Profiles</h2>
<p>When creating plugins the profiles need to be adjusted. We show here what changes need to be done.</p>
<p>The profile in the echoplugin directory uses the <tt>subdirs</tt> template and simply includes includes to directories in which the echo window and echo plugin lives:</p>
<pre> TEMPLATE    = subdirs
 SUBDIRS     = echowindow \
               plugin</pre>
<p>The profile for the echo window does not need any plugin specific settings. We move on to the plugin profile:</p>
<pre> TEMPLATE        = lib
 CONFIG         += plugin
 INCLUDEPATH    += ../echowindow
 HEADERS         = echoplugin.h
 SOURCES         = echoplugin.cpp
 TARGET          = echoplugin</pre>
<p>We need to set the TEMPLATE as we now want to make a library instead of an executable. We also need to tell qmake that we are creating a plugin. The <tt>EchoInterface</tt> that the plugin implements lives in the <tt>echowindow</tt> directory, so we need to add that directory to the include path. We set the TARGET of the project, which is the name of the library file in which the plugin will be stored; qmake appends the appropriate file extension depending on the platform. By convention the target should have the same name as the plugin (set with <a href="qtplugin.html#Q_EXPORT_PLUGIN2#q-export-plugin2">Q_EXPORT_PLUGIN2</a>)</p>
<a name="further-reading-and-examples"></a>
<h2>Further reading and examples</h2>
<p>You can find an overview of the macros needed to create plugins <a href="qtplugin.html">here</a>.</p>
<p>We give an example of a plugin that extend Qt in the <a href="tools-styleplugin.html">style plugin</a> example. The <a href="tools-plugandpaint.html">plug and paint</a> example shows how to create static plugins.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
